fix(memberships): skip team-owned memberships in transfer_membership#314
Open
fix(memberships): skip team-owned memberships in transfer_membership#314
Conversation
`Woocommerce_Membership_Updated::transfer_membership()` rewrites `post_author` on the user_membership to reassign ownership. For team-owned memberships (those with a `_team_id` meta), that's insufficient: true ownership lives on the team post's `_member_id`, the linked subscription's `_customer_user`, and team_member user meta. Rewriting only `post_author` silently desyncs those and can trigger duplicate-team creation on the next renewal dispatch. Skip the transfer (with a log note) when the membership carries `_team_id`. Team ownership transfers must be handled at the team level, not via the user_membership post. Refs NPPM-2741. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Guards Woocommerce_Membership_Updated::transfer_membership() against corrupting SkyVerge Teams “team-owned” memberships by skipping transfers when a _team_id meta is present, and adds a unit test to cover the new behavior.
Changes:
- Detects
_team_idon an existing managed membership during transfer handling and exits early with a debug log instead of rewritingpost_author. - Adds a unit test ensuring
post_authorremains unchanged for team-owned memberships.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
includes/incoming-events/class-woocommerce-membership-updated.php |
Adds a guard to skip transferring team-owned memberships (identified by _team_id) and logs the skip to avoid ownership desync. |
tests/unit-tests/test-membership-transfer.php |
Adds a unit test verifying team-owned memberships are not transferred via post_author rewrite. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes proposed in this Pull Request:
Refs NPPM-2741.
Woocommerce_Membership_Updated::transfer_membership()currently reassigns a user_membership to its new owner by rewritingpost_authorviawp_update_post. That's fine for standalone memberships, but for team-owned memberships (those with a_team_idmeta) ownership doesn't live onpost_author– it lives on three separate records:_member_id(the team owner)_customer_userRewriting only
post_authorsilently desyncs all of those. On the next renewal dispatch, SkyVerge Teams' owner-vs-customer check can then spawn a duplicate team, and the original team/membership pair gets orphaned.Today this path is hard to hit because SkyVerge Teams does not fire
wc_memberships_user_membership_transferred, soEvents::membership_transferrednever dispatches the network event for team memberships from the node side. But a future Teams version, a manual dispatch, a mis-linked remote membership, or a reconciliation flow could all reachtransfer_membershipwith a team-owned local membership, and today that would silently corrupt the data. This PR makes the function skip team-owned memberships and log a note instead.Cheapest guard:
get_post_meta( $existing_membership_id, '_team_id', true )– direct meta read, no object instantiation.How to test the changes in this Pull Request:
Unit tests – a new test
test_transfer_skips_team_owned_membershipis added alongside the existingTestMembershipTransfersuite. It sets up a managed user_membership with_team_id=42meta, invokestransfer_membership()via Reflection, and assertspost_authoris unchanged.Expected:
OK (5 tests, 7 assertions).Other information:
test_transfer_skips_team_owned_membership)🤖 Generated with Claude Code